#include "SignalManager.h"

std::atomic<SignalManager*> SignalManager::m_load;
std::mutex SignalManager::m_mutex; 
SignalManager* SignalManager::m_instance = nullptr;

SignalManager::SignalManager()
{
	
}

SignalManager::~SignalManager()
{
	for (auto var : m_signals)
	{
		delete var.second;
		var.second = nullptr;
	}
	m_signals.clear();
}

Slot* SignalManager::add(const int& messageId, EventHandler* evHandler, const bool& once)
{
	if (m_signals.find(messageId) == m_signals.end())
		m_signals[messageId] = new Signal();
	if (!once)
		return m_signals[messageId]->add(evHandler);
	else
		return m_signals[messageId]->addOnce(evHandler);
}

Slot * SignalManager::add(const int & messageId,  std::function<void(Json::Value&)>& func, const bool & once)
{
	if (m_signals.find(messageId) == m_signals.end())
		m_signals[messageId] =new Signal();
	if (!once)
		return m_signals[messageId]->add(func);
	else
		return m_signals[messageId]->addOnce(func);
}

void SignalManager::dispatch(const int& messageId, Json::Value& message)
{
	if (m_signals.find(messageId) != m_signals.end())
		m_signals[messageId]->dispatch(message);
}